#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2023/4/26 16:14
   @File: redisUtil.py
"""

import redis

from BladeFerm.Lib.Common import logger


class RedisUtil:
    def __init__(self, ip, password='', port=6379, db=0):
        self.ip = ip
        self.password = password
        self.port = port
        self.db = db
        self.client = None

    def conn(self):
        pool = redis.ConnectionPool(host=self.ip, port=self.port, password=self.password, db=self.db)
        self.client = redis.Redis(connection_pool=pool)
        logger.info('连接成功')
    
    def delete_key(self, key_word):
        """"
        Args:
            key_word: eg: key = hsrcm-rcce-risk:600001106
        Returns:
        """
        self.client.delete(key_word)
        logger.info(f'=======删除redis-key:{key_word}成功==========')

    def get_key(self, key_word):
        return self.client.get(key_word)

    def close_connect(self):
        self.client.close()

    @staticmethod
    def redis_delete_key(config, key):
        RU = RedisUtil(**config)
        RU.conn()
        RU.delete_key(key)
        RU.close_connect()
